﻿module Sustainalytics.Accounts.Admin {
    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;
    import Routing = Sustainalytics.Routing;
    export class IpAuthenticationController {

     
        public status: string;
        public searchText: string;
        public accountList:AccountsDto[];

        constructor(
            private $scope,
            private $rootScope,
            private accountFactory: AccountsService
            
            ) {

            var ctx = this;
            ctx.status = "All";
            ctx.searchText = "";
            ctx.InitStuff();
            ctx.accountFactory.accountsGetCount(ctx.status, "").then(a => {

                if (!ctx.$scope['pager']) return;
                ctx.$scope['pager'].totalItemsCount = a;
            });
            ctx.$scope.$on('pagerChanged', function (event, pager) {
                // get accounts
                ctx.InitStuff();
            });
            
        }

        private statusChange() {
            var ctx = this;
            ctx.InitStuff();
        }

        private InitStuff(): void {
            var ctx = this;
            var listOptions = new CommonAdmin.HttpGetOptionsDto();

            // get pager
            var pager: Sustainalytics.Pager.PagerController = ctx.$scope['pager'];

            // If pager is not defined then abort execution.
            // Current controller is created before Pager controller.
            if (!pager) {
                ctx.$rootScope.loading = false;
                return;
            }

            listOptions.skip = pager.pagingOptions.pageSize * (pager.pagingOptions.pageIndex - 1);
            listOptions.take = pager.pagingOptions.pageSize;
            //listOptions.orderBy = [ctx.$scope.orderby];
            //listOptions.orderAscending = !ctx.$scope.descending;

            ctx.accountFactory.accountsGetList(listOptions, ctx.status, ctx.searchText)
                .then(data => {
                    ctx.accountList = data;
                //ctx.$scope.count = data.length;
            }).then(a => {
                ctx.accountFactory.accountsGetCount(ctx.status, ctx.searchText).then(a => {
                    ctx.$scope['pager'].totalItemsCount = a;
                });
                });
        }
    }
    var mod = angular.module(accountAdminModuleName);
    mod.controller('IpAuthenticationController', ['$scope','$rootScope', 'AccountsService', IpAuthenticationController]);
}